package com.nullpointer.playerinfo.bukkit;

import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredListener;

import com.nullpointer.playerinfo.bukkit.events.PlayerJoin;
import com.nullpointer.playerinfo.bukkit.events.PlayerQuit;

public class EventManager {
    private static final Listener playerJoinEvent = new PlayerJoin();
    private static final Listener plaeyrQuitEvent = new PlayerQuit();

    public static void registerEvent() { //可根据配置文件注册或注销事件，节省性能
        PluginManager pluginManager = Bukkit.getPluginManager();

        if (isNotRegistered(playerJoinEvent)) { //未注册则注册
            pluginManager.registerEvents(playerJoinEvent, PlayerInfoBukkit.plugin);
        }
        if (isNotRegistered(plaeyrQuitEvent)) {
            pluginManager.registerEvents(plaeyrQuitEvent, PlayerInfoBukkit.plugin);
        }
    }

    private static boolean isNotRegistered(Listener listener) { //返回事件是否未注册
        List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(PlayerInfoBukkit.plugin);
        for (RegisteredListener registeredListener : listeners) {
            if (registeredListener.getListener() == listener) {
                return false;
            }
        }
        return true;
    }
}
